package leetcode;

/**
 * @创建人 MBY381
 * @创建时间 2022/5/19 14:34
 * @描述 暂无
 */
public class Solution19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head==null){
            return null;
        }
        ListNode first=head;
        ListNode behind=new ListNode(-1);
        behind.next=head;
        while(n>0){
            first=first.next;
            n--;
        }
        while(first!=null){
            first=first.next;
            behind=behind.next;
        }
        if(behind.next==head){
            return head.next;
        }else{
            behind.next=behind.next.next;
            return head;
        }
    }
}
